import requests
import json
import base64
import random

from django.http import HttpResponse
from django.views.generic import View
from django.shortcuts import render
from django.core import serializers
from django.http import JsonResponse

from wedding_info.models import Wedding_Info
from message.models import Message
from user.models import User
from wedding_info.tests import get_qrcode

# Create your views here.


def get_access_token():
    appip = 'wxeeed10601755ca5a'
    scret = '057ed79bc58797e7d78a12deae4bc573'
    url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}'.format(appip, scret)
    res = requests.get(url)
    return res.json()['access_token']


class ScanCodeView(View):
    def post(self, request):
        scene = request.POST.get('scene', '')
        b64str = get_qrcode(scene)
        return HttpResponse(b64str, content_type='application/json')


# 婚礼详情页面
class Wedding_Detail(View):
    def get(self, request, wedding_id):
        scene = wedding_id
        b64str = get_qrcode(scene)

        wedding = Wedding_Info.objects.filter(wedding_id=int(wedding_id))
        for wed in wedding:
            wed.qr_code = b64str
            wed.save()
        all_messages = Message.objects.filter(wedding__wedding_id=int(wedding_id))

        json_data1 = serializers.serialize('json', all_messages)
        json_data2 = serializers.serialize('json', wedding)

        json_data1 = json.loads(json_data1)
        json_data2 = json.loads(json_data2)


        return JsonResponse(json_data1+json_data2, safe=False)

    def post(self, request, wedding_id):
        openid = request.POST.get('openid', '')
        wedding_message = request.POST.get('message', '')
        message = Message()
        message.message = wedding_message
        message.user.open_id = openid
        message.wedding.wedding_id = wedding_id
        message.save()
        return HttpResponse("发送成功")


class ScanCodeView(View):
    def post(self, request):
        js_code = request.POST.get('js_code', '')
        appid = 'wxeeed10601755ca5a'
        scret = '057ed79bc58797e7d78a12deae4bc573'
        try:
            url = 'https://api.weixin.qq.com/sns/jscode2session?appid={0}&secret={1}&js_code={2}&grant_type=authorization_code'.format(appid, scret, js_code)
            res = requests.get(url)
            openid = res.json()['openid']
            user = User()
            user.open_id = openid
            user.save()
            data = {
                'openid': openid
            }
            return HttpResponse(json.dumps(data))

        except Exception as e:
            return HttpResponse("登录出错")


class GenericWeddingView(View):
    #生成婚礼的视图函数
    def get(self, request):
        wedding = Wedding_Info()
        wedding_id = random.randint(0, 999999999)
        wedding.wedding_id = wedding_id
        wedding.save()
        data = {
            'url': "wedding/{0}".format(wedding_id)
        }
        return HttpResponse(json.dumps(data))

    def post(self, request, wedding_id):
        try:
            image = request.FILES
            wedding = Wedding_Info.objects.get(wedding_id=int(wedding_id))
            wedding.backgroud_image = image
            wedding.save()
            return HttpResponse("添加成功")
        except Exception as e:
            return HttpResponse("添加失败")



